import { VCourseSlotScens ,CourseSlotReq,CourseSlotResp} from '../../types/vCourseSlotScens'
import { select } from '../../utils/db'
import {CourseTimeDao} from './courseTimeDao'
import { TrainerCourseDao } from '../dao/trainerCourseDao'
import { TrainerCourse } from '../../types/trainerCourse'
//import { Page, PageReq } from '../../types/page'

export class VCourseSlotScensDao {
  /**
   * 获取视图详情
   */
  static async getById(id: number): Promise<VCourseSlotScens> {
    const result = await select<VCourseSlotScens>(
      'SELECT * FROM v_course_slot_scens WHERE id = ? LIMIT 1',
      [id]
    )
    return result[0]
  }

  static async list(params: CourseSlotReq): Promise<CourseSlotResp[]> {
    const { date, train_doctor_id, secens_id } = params
    const fields = []
    const values = []
   // console.log("params",params)
    if(date){
      fields.push('course_date = ?')
      values.push(date)
    }
    if(train_doctor_id){
      fields.push('train_doctor_id = ?')  
      values.push(train_doctor_id)
    }
    if(secens_id){
      fields.push('secens_id = ?')
      values.push(secens_id)
    }
    //trainers
   // if(params.trainers_id){
    //  fields.push('trainers_id IN (?)')
    //  values.push(params.trainers_id)
    //}
    const condition = fields.length > 0 ? `WHERE ${fields.join(' AND ')}` : ''
    const sql = `SELECT * FROM v_course_slot_scens ${condition}`
    //console.log(sql,values)
    const result = await select<VCourseSlotScens[]>(sql,values)
    const course_time = await CourseTimeDao.list()
    //console.log(course_time)
    const resp:CourseSlotResp = {
      data: date,
      tag: result.length>0? '*':'',
      content: [] as VCourseSlotScens[]
    }
    //console.log("result",result)

    for (const item of course_time) {
     // console.log(item)
     // console.log(result)
      if(result && result.length > 0){
        // result 中是否有 course_time_id 等于 item.id 的
        const itemResult = result.find(item2=>item2.course_time_id == item.id)
        if(itemResult){
          //从trainer_couser中获取trainer_id
          const trainerCourses = await TrainerCourseDao.list({ slot_id: itemResult.id ,course_id: itemResult.course_id} as TrainerCourse)
          //console.log("trainerCourses",trainerCourses)
          const trainers = []
          trainerCourses.forEach(item=>{
            if(params.trainers_id){
              if(params.trainers_id === item.trainer_id){
                trainers.push(item.trainer_id)
              }
            }else{
             trainers.push(item.trainer_id)
            }
          })
          if(!params.trainers_id){
            itemResult.trainers = trainers
            itemResult.course_date = date
            resp.content.push(itemResult)
          }else{
            if(trainers && trainers.length>0){
              itemResult.trainers = trainers
              itemResult.course_date = date
              resp.content.push(itemResult)
            }else{
              resp.content.push({
                id: 0,
                course_id: null,
                train_doctor_id: null,
                train_doctor_name: null,
                course_date: date,
                course_time_id: item.id,
                course_time_name: item.name,
                ar_duration: null,
                sound_bars: null,
                start_time: item.start_time,
                end_time: item.end_time,
                secens_id: null,
                scens_code: null,
                scens_name: null,
                trainers: []
              } as VCourseSlotScens)                                  
            }
          }
        }else{
          resp.content.push({
            id: 0,
            course_id: null,
            train_doctor_id: null,
            train_doctor_name: null,
            course_date: date,
            course_time_id: item.id,
            course_time_name: item.name,
            ar_duration: null,
            sound_bars: null,
            start_time: item.start_time,
            end_time: item.end_time,
            secens_id: null,
            scens_code: null,
            scens_name: null,
            trainers: []
          } as VCourseSlotScens)
        }
      }else{
        resp.content.push({
            id: 0,
            course_id: null,
            train_doctor_id: null,
            train_doctor_name: null,
            course_date: date,
            course_time_id: item.id,
            course_time_name: item.name,
            ar_duration: null,
            sound_bars: null,
            start_time: item.start_time,
            end_time: item.end_time,
            secens_id: null,
            scens_code: null,
            scens_name: null,
            trainers: []
          } as VCourseSlotScens)
      }
    }
     //console.log([resp])
    return [resp]
  }
  /**
   * 分页查询视图列表
   */
  /** 
  static async page(params: PageReq<Partial<VCourseSlotScens>>): Promise<Page<VCourseSlotScens>> {
    const { pageNum, pageSize } = params
    const offset = (pageNum - 1) * pageSize
    const fields = []
    const values = []
    const queryParams = params.params
    
    if (queryParams.course_id) {
      fields.push('course_id = ?')
      values.push(queryParams.course_id)
    }
    if (queryParams.train_doctor) {
      fields.push('train_doctor LIKE ?')
      values.push(`%${queryParams.train_doctor}%`)
    }
    if (queryParams.course_time_id) {
      fields.push('course_time_id = ?')
      values.push(queryParams.course_time_id)
    }
    if (queryParams.course_time_name) {
      fields.push('course_time_name LIKE ?')
      values.push(`%${queryParams.course_time_name}%`)
    }
    if (queryParams.scens_name) {
      fields.push('scens_name LIKE ?')
      values.push(`%${queryParams.scens_name}%`)
    }

    const condition = fields.length > 0 ? `WHERE ${fields.join(' AND ')}` : ''
    const [totalResult, listResult] = await Promise.all([
      select('SELECT COUNT(*) as count FROM v_course_slot_scens ' + condition, values),
      select(`SELECT * FROM v_course_slot_scens ${condition} LIMIT ${pageSize} OFFSET ${offset}`, values)
    ])

    return {
      total: totalResult[0].count || 0,
      list: (listResult as unknown as VCourseSlotScens[]) || []
    }
  }*/
}